import { getTagList, getUtilization } from '@/api/advanced'

const state = {
  tagLoading: false,
  tagTable: [],
  tagCloneResList: [], // 收集所有的克隆资源可选择资源
  utilizeLoading: false,
  NodeUtilization: [],
  ResUtilization: []
}

const mutations = {
  SET_TAG_LOADING(state, loading) {
    state.tagLoading = loading
  },
  SET_TAG_TABLE(state, data) {
    state.tagTable = data
  },
  SET_TAG_CLONE_RES_LIST(state, data) {
    state.tagCloneResList = data
  },
  SET_UTILIZE_LOADING(state, loading) {
    state.utilizeLoading = loading
  },
  SET_NODE_UTILIZATION(state, data) {
    state.NodeUtilization = data
  },
  SET_RES_UTILIZATION(state, data) {
    state.ResUtilization = data
  }
}

const actions = {
  // 获取Tag列表数据
  fetchTagData({ commit }) {
    commit('SET_TAG_LOADING', true)
    commit('SET_TAG_TABLE', [])
    commit('SET_TAG_CLONE_RES_LIST', [])

    return getTagList().then((res) => {
      commit('SET_TAG_LOADING', false)
      if (res.data.action) {
        commit('SET_TAG_TABLE', res.data.data || [])
        commit('SET_TAG_CLONE_RES_LIST', res.data.res_list || [])
        return res.data
      } else {
        return Promise.reject(res.data.error)
      }
    }).catch((error) => {
      commit('SET_TAG_LOADING', false)
      throw error
    })
  },
  // 获取利用率数据
  fetchUtilizationData({ commit }) {
    commit('SET_UTILIZE_LOADING', true)

    return getUtilization().then((res) => {
      commit('SET_UTILIZE_LOADING', false)
      if (res.data.action) {
        const data = res.data.data
        const nodeUtilization = []
        const resUtilization = []

        // 处理节点利用率数据
        if (data.NodeUtilization) {
          data.NodeUtilization.forEach((item, index) => {
            const attriKeys = Object.keys(item.attri)
            if (attriKeys.length > 0) {
              attriKeys.forEach((attrItem, attrIndex) => {
                nodeUtilization.push({
                  id: `${index}-${attrIndex}`,
                  name: item.name,
                  attri: attrItem,
                  value: item.attri[attrItem],
                  editValue: item.attri[attrItem],
                  isEdit: false,
                  isError: false
                })
              })
            }
          })
        }

        // 处理资源利用率数据
        if (data.ResUtilization) {
          data.ResUtilization.forEach((item, index) => {
            const attriKeys = Object.keys(item.attri)
            if (attriKeys.length > 0) {
              attriKeys.forEach((attrItem, attrIndex) => {
                resUtilization.push({
                  id: `${index}-${attrIndex}`,
                  name: item.name,
                  attri: attrItem,
                  value: item.attri[attrItem],
                  editValue: item.attri[attrItem],
                  isEdit: false,
                  isError: false
                })
              })
            }
          })
        }

        commit('SET_NODE_UTILIZATION', nodeUtilization)
        commit('SET_RES_UTILIZATION', resUtilization)
        return res.data
      } else {
        return Promise.reject(res.data.error)
      }
    }).catch((error) => {
      commit('SET_UTILIZE_LOADING', false)
      throw error
    })
  }
}

const getters = {
  tagLoading: state => state.tagLoading,
  tagTable: state => state.tagTable,
  tagCloneResList: state => state.tagCloneResList,
  utilizeLoading: state => state.utilizeLoading,
  NodeUtilization: state => state.NodeUtilization,
  ResUtilization: state => state.ResUtilization
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
